package com.example.administrator.t10_4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String[] countriesStr ={"广州","从化","武汉","汕头"};
    private TextView myTextView;
    private EditText myEditText;
    private Button myButton_add;
    private Button myButton_remove;
    private Spinner mySpinner;
    private ArrayAdapter adapter;
    private List allCountries;
    Animation myAnimation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        allCountries = new ArrayList();
        for (int i=0;i<countriesStr.length;i++)
        {
            allCountries.add(countriesStr[i]);
        }
        myTextView = (TextView)findViewById(R.id.myTextView);
        myEditText = (EditText)findViewById(R.id.myEditText);
        myButton_add = (Button)findViewById(R.id.myButton_add);
        myButton_remove =(Button)findViewById(R.id.myButton_remove);
        mySpinner = (Spinner)findViewById(R.id.mySpinner);
        adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,countriesStr);
        adapter.setDropDownViewResource(R.layout.myspinner_dropdown);
        mySpinner.setAdapter(adapter);
        myButton_add.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0)
            {
                String newCountry = myEditText.getText().toString();
                for(int i=0;i<adapter.getCount();i++)
                {
                    if(newCountry.equals(adapter.getItem(i)))
                    {
                        return;
                    }
                }
                if(!newCountry.equals(""))
                {
                    adapter.add(newCountry);
                    int position = adapter.getPosition(newCountry);
                    mySpinner.setSelection(position);
                    myEditText.setText("");
                }
            }
        });
        myButton_remove.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0)
            {
                if(mySpinner.getSelectedItem()!=null)
                {
                    adapter.remove(mySpinner.getSelectedItem().toString());
                    myEditText.setText("");
                    if(adapter.getCount()==0)
                    {
                        myTextView.setText("");
                    }
                }
            }
        });
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView arg0, View arg1,int arg2,long arg3){
                myTextView.setText("\n选择的是"+countriesStr[arg2]);
                arg0.setVisibility(View.VISIBLE);
            }
            @Override
            public void onNothingSelected(AdapterView arg0)
            {

            }
        });
        myAnimation = AnimationUtils.loadAnimation(this,R.anim.my_anim);
        mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                v.startAnimation(myAnimation);
                v.setVisibility(View.INVISIBLE);
                return false;
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.spinner_demo1,menu);
        return true;
    }
}